<?php
    include "../../function/config.php";
    include "../../function/functionPHP.php";
    $mysqli=  connect();
    $query="
        select      *
        from        ".COOP_TABLE."
        where       coopName like '%".$_POST["keyword"]."%'
        order by    coopName
        ";
    $result=$mysqli->query($query);
    while($row=$result->fetch_array(MYSQLI_ASSOC)){
        $queryMB="
            select  *
            from    member
            where   coopID=".$row["coopID"]."
            ";
        $resultMB=$mysqli->query($queryMB);
        $numrowsMB=$resultMB->num_rows;
        ?>
<div class="mainMenu hover" 
    onclick="
        map<?=$row["coopID"]?>();
        $('.mainMenu').removeClass('mainMenuActive');
        $(this).addClass('mainMenuActive');
    ">
    <mainMenu>
        <img src="img/coop.png" height="30">
        <?=$row["coopName"]?><br>
        <div style="font-size:80%;text-align: right;">
            <?php
                if($numrowsMB>0){
                    echo "สมาชิก $numrowsMB คน";
                }else{
                    echo "ไม่มีสมาชิกเกษตรกร";
                }
            ?>
        </div>
    </mainMenu>
</div>
<script type="text/javascript">
    function map<?=$row["coopID"]?>(){
        var geocoder = new google.maps.Geocoder();
        var locations = [
            <?php
                $iMB=0;
                unset($lat);
                unset($long);
                if($numrowsMB>0){
                    while($rowMB=$resultMB->fetch_array(MYSQLI_ASSOC)){
                        $geo=  explode(",",$rowMB["geoLocation"]);
                        ?>['<?=$rowMB["title"]." ".$rowMB["firstName"]." ".$rowMB["lastName"]?>',<?=$geo[0]?>,<?=$geo[1]?>]<?php
                        if($iMB+1<$numrowsMB){
                            echo ",";
                        }

                        if($geo[0]!=""){
                            $lat[]=$geo[0];
                        }
                        if($geo[1]!=""){
                            $long[]=$geo[1];
                        }

                        $i++;
                    }
                    $midLat=(max($lat)+min($lat))/2;
                    $midLong=(max($long)+min($long))/2;
                }else{
                    $coopGeo=  explode(",", $row["geoLocation"]);
                    $midLat=$coopGeo[0];
                    $midLong=$coopGeo[1];
                }
            ?>
        ];

        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 12,
            center: new google.maps.LatLng(<?=$midLat?>, <?=$midLong?>),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var infowindow = new google.maps.InfoWindow();

        var marker, i;

        for (i = 0; i < locations.length; i++) {  
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                map: map
            });

            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(locations[i][0]);
                    infowindow.open(map, marker);
                }
            })(marker, i));
        }
    }
</script>
            <?php
    }
?>